package com.zwz.crawler.basic;

import com.zwz.constant.Constant;
import com.zwz.entity.EnterpriseInfo;
import com.zwz.parse.JsonParserUtil;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import static com.zwz.conf.Dbconf.*;

@Slf4j
public class TungeeEnterpriseInfo {



    public static EnterpriseInfo startTungeeEnterpriseInfo(String  enterpriseId) {
        // 构建带参数的URL
//        String enterpriseId = "577de19eadf21dc4";
        String url = "https://sales.tungee.com/api/enterprise/info/basic?enterprise_id=" + enterpriseId;

        try {
            Connection.Response response = Jsoup.connect(url)
                    .method(Connection.Method.GET)
                    // 设置请求头（关键头信息）
                    .header("authority", "sales.tungee.com")
                    .header("scheme", "https")
                    .header("accept", "*/*")
                    .header("accept-encoding", "gzip, deflate, br, zstd")
                    .header("accept-language", "zh-CN,zh;q=0.9")
                    .header("cache-control", "no-cache")
                    .header("pragma", "no-cache")
                    .header("priority", "u=1, i")
                    .header("referer", "https://sales.tungee.com/enterprise-details/577de19eadf21dc4/enterprise-information/basic-information")
                    .header("sec-ch-ua", "\"Not;A=Brand\";v=\"24\", \"Chromium\";v=\"128\"")
                    .header("sec-ch-ua-mobile", "?0")
                    .header("sec-ch-ua-platform", "\"Windows\"")
                    .header("sec-fetch-dest", "empty")
                    .header("sec-fetch-mode", "cors")
                    .header("sec-fetch-site", "same-origin")
                    .header("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36")
                    .header("x-tonxis-pid", "67864ea75f69e05d3b544ca7")
                    .header("x-tonxis-sid", "F0UwEUNiJWeGTqdfaeBdO1RMp4LWlJFkZWg/tJKfJ3SJ0e8=")
                    .header("x-tonxis-signature", "aVm5EXMn8C148XMOE4MOMGc7FP2rFG8sRPjI8PF4E4trRhcw8G1sE6iasFvx.rEdoy9hY7BQt620YYgNObF./V1/ypkGmqLgW9uRZ/vZ/LwkhVD.menEvID/VM9ZR8OjQYLb5+QADV.GQ6OkdzZ.k+EgwOIc28KPVEHCcM7lQ7CFlV7owQ1XtVk26pct1Z26GmgO2hGmA6KDweLWlt4ZqzR7TCk/7hGoU7S70lLiSkZXv0W.em5kupVu")
                    // 设置完整 Cookie（从浏览器复制最新值）
//                    .header("cookie", " _tx_pid=67864ea75f69e05d3b544ca7; __wpkreporterwid_=cc4a07fc-7bb9-40a4-96bc-2cbac34b658f; __last_enter_version=sales; _co_i=6805a5576eceb9637696f09d; _tx_uid=680701dddca941522b77e864; _tx_cid=6805a5576eceb9637696f09d; DISTINCT_ID=b84fe276-099f-4932-92b3-d54b92ae384b; _tx_sid=MTc0NTMwMTE0MzYyMjo4MmQ2OTQ5MTY0NjU2ODNmYjQ5MjlmMjc3NDg5ZDFlZg==; SecurityCenterDuId=ImFBSG15RzdPdVdPdCtsWnNGMFVuSXhVPSI.GujGMQ.drG94beetGxPTF2QwZEbncLE74I; remember_token=680734b1dca9415227c304ff|c3627fddf0071ffcba037dc9a78db2751dfcbda40933b7b1a5c8b4931dffe8288c043aaa80501e311535299f4fe7aa79cc677163a303cfef2f3f7e0d612681cb; Hm_lvt_f2ee75449fc055cc4dbceb4fe403bea3=1745301094,1745302709; HMACCOUNT=B4E402DF057C895E; tg_referrer_source=https%3A%2F%2Fcrm.tungee.com%2F; acw_tc=1a0c385017453029943822778e00767718bc3a89ed169ec00ece437e79555a; Hm_lpvt_f2ee75449fc055cc4dbceb4fe403bea3=1745303469; accountCenterSessionId=.eJwlzkFqw0AMheG7zDoLaSR7JrlAKdQpBUOwN0Yz0iTBSQq200JK71633b_38325-2zTcFa3c3WEQJxQs2wZK-9DJuBS3MbdzHSY5cOG5X3Q5HZFLrNtXD7J7WaX9fxpad39h0goloiYwEhr0oIl1mioliyVgMFIYvZJgYWNOeccghCAB8mwBYBQImmMLMJIIZEUqXJgnysgScoVKKYaOIKvGZXNgy9Ga1kD2S-kTDaf3G6Z7itz_mM112fcP71V3WNc-vbouzNA04740h6pOzRL147UX_vr_tHg66Ej9_0DNPlUig.GujJLg.6b_KiUVyDRdX9hIqVcCwlAXoyO0; doncusSessionId=eyJfZnJlc2giOmZhbHNlLCJ1c2VyX2lkIjoiNjgwNzAxZGRkY2E5NDE1MjJiNzdlODY0In0.GujJNw.4SUB2jh7PIU77UZzoV5OtmfBeA0 ")
                    .header("cookie", Constant.cookies)
                    .ignoreContentType(true)  // 允许处理 JSON 响应
                    .ignoreHttpErrors(true)   // 避免直接抛出 HTTP 异常
                    .maxBodySize(0)           // 禁用默认的 1MB 响应限制
                    .execute();

           log.info("HTTP 状态码: " + response.statusCode());
//           log.info("响应内容:\n" + response.body());
            // 处理认证失败场景
            if (response.statusCode() == 401) {
                System.err.println("认证失败可能原因：");
                System.err.println("1. Cookie 已过期（需重新登录获取）");
                System.err.println("2. x-tonxis-signature 需要动态生成");
                System.err.println("3. enterprise_id 参数无效或权限不足");
            }



            //处理解析：
      return       JsonParserUtil.parseEnterpriseInfo(response.body());



        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }






}